import MySql.User;
import MySql.UserManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        out.println("欢迎");

        String username=request.getParameter("username");
        String passwd=request.getParameter("passwd");
        String email=request.getParameter("email");
        String truename=request.getParameter("truename");
        int age=Integer.parseInt(request.getParameter("age"));


        User u=new User();
        u.setUsername(username);
        u.setPasswd(passwd);
        u.setEmail(email);
        u.setTrueName(truename);
        u.setAge(age);

        try{
            if (UserManager.registerUser(u)){
                out.println(username+"恭喜你，注册成功! 2秒内会返回登录页面");
                response.setHeader("Refresh","2;url=login.jsp");
            }else{
                out.println("对不起，注册失败，请重新注册,1秒内会返回注册页面");
                response.setHeader("Refresh","1;url=register.jsp");
            }
        }catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
